/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package file;

import com.sun.squawk.io.BufferedReader;
import com.sun.squawk.microedition.io.FileConnection;
import com.sun.squawk.util.StringTokenizer;
import edu.wpi.first.wpilibj.templates.MessageCenter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.microedition.io.Connector;

/**
 *
 * @author Conner Davis - Team 708
 */
public class AutonomousScript implements Configurable{


    private Configuration config;
    private int mode = 0;
    private String[] script = new String[100];
    private int[] modesInScript = new int[12];
    private int nextCommand = 0;
    private int lastModeIndex = 0;
    private int currentModeIndex = 0;
    private FileConnection fcConfigFile;
    private InputStream ist;
    private BufferedReader istream;
    private InputStreamReader istr;
    MessageCenter messageCenter;

    private static AutonomousScript m_instance;

    public static AutonomousScript getInstance() {
	if (m_instance == null) {
	    m_instance = new AutonomousScript();
	}
	return m_instance;
    }

    private AutonomousScript() {
        try{
            modesInScript[0] = 0;
            fcConfigFile = (FileConnection) Connector.open("file:////ni-rt/startup/autoscripts2011.txt",
                                                                      Connector.READ);
            ist = fcConfigFile.openInputStream();
            istr = new InputStreamReader(ist);
            istream = new BufferedReader(istr);
            System.out.println("Script file opened");         
            checkModes();
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }

        config = Configuration.getInstance();
        config.register("Autonomous", this);
    }

    public int getMode()
    {
        return mode;
    }

    public void setMode(int newMode)
    {
        if (newMode <= modesInScript[lastModeIndex])
        {
            mode = newMode;
        }
        else
        {
            System.out.println("Invalid auto mode in config file.");
        }
    }

    public void nextMode()
    {
        if (currentModeIndex == lastModeIndex)
        {
            currentModeIndex = 0;
        }
        else
        {
            currentModeIndex++;
        }
        mode = modesInScript[currentModeIndex];
        messageCenter.println(MessageCenter.kConfigScreen, MessageCenter.kLine1, "Auto Mode: " + mode);
    }

    public void prevMode()
    {
        if (currentModeIndex == 0)
        {
            currentModeIndex = lastModeIndex;
        }
        else
        {
            currentModeIndex--;
        }
        mode = modesInScript[currentModeIndex];
        messageCenter.println(MessageCenter.kConfigScreen, MessageCenter.kLine1, "Auto Mode: " + mode);
    }

    public void processScript()
    {
        try
        {
        //  InputStream ist = new FileInputStream("E:\\Robotics code\\autoscripts2010.txt");

        fcConfigFile = (FileConnection) Connector.open("file:////ni-rt/startup/autoscripts2011.txt",
                                                                      Connector.READ);
        ist = fcConfigFile.openInputStream();
        istr = new InputStreamReader(ist);
        istream = new BufferedReader(istr);

        int lineNumber = 0;
        int intMode = 0;
        String line = istream.readLine(); // read the first line in the text file

        while ( line != null ) // while more lines in the file
        {
            String thisMode;

            StringTokenizer st = new StringTokenizer(line);

            if (st.hasMoreTokens()) {

                thisMode = st.nextToken();
                try
                  {
                     intMode = Integer.parseInt(thisMode);
                  }
                      catch (NumberFormatException e) // if line starts without a number
                     {
                        System.out.println("Script file contains a line that does not start"
                           + " with a mode number\rSkipping to next line");
                     }

                if (intMode == mode) {
                    script[lineNumber] = line.substring(thisMode.length() + 1);
                    lineNumber++;
                }
            }
            line = istream.readLine();
        } // end of while ( line != null )

        if (lineNumber == 0) {
            System.out.println("No script found for mode " + mode);
        }
        else {
            if (!script[lineNumber - 1].equalsIgnoreCase("Halt")) {
                script[lineNumber] = "Halt";
            }
        }

        ist.close();
        }
//        catch (FileNotFoundException e)
//        {
//         System.out.println(e.getMessage());
//        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
        try{
        istr.reset();
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
private void checkModes()
{
    int intMode = 0;
  try{
    String line = istream.readLine();
    while (line != null)
    {
        String thisMode;

        StringTokenizer st = new StringTokenizer(line);

        if (st.hasMoreTokens()) {

            thisMode = st.nextToken();
            try
              {
                 intMode = Integer.parseInt(thisMode);
              }
                  catch (NumberFormatException e) // if line starts without a number
                 {
                    System.out.println("Script file contains a line that does not start"
                       + " with a mode number\rSkipping to next line");
                 }
            if (intMode != modesInScript[lastModeIndex])
            {
                modesInScript[++lastModeIndex] = intMode;
            }
        }
        line = istream.readLine();
    }
    istr.close();

    
    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }
}
    public String getNextCommand()
    {
        String command = script[nextCommand];
        nextCommand++;
        return command;
    }

    public boolean hasNextCommand()
    {
        return (script[nextCommand] != null);
    }

    public void update(String name, String[] values) {
        if(name.equals("Mode.Default")){
            int val = Integer.parseInt(values[0]);

            setMode(val);
            currentModeIndex = val;
            System.out.println("Auto mode: " + val);
        }
    }

}